import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;


public class Filter {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Filter f = new Filter();
		System.out.println("Rozpoczynam filtrowanie");
		f.separate();
		System.out.println("Koniec filtrowania");

	}
	
	public void clear(){
		HashSet<Integer> correct = new HashSet<Integer>();
		try {
			Scanner train = new Scanner(new File("resources/train.csv"));
			Scanner test = new Scanner(new File("resources/test.csv"));
			PrintWriter out = new PrintWriter(new File("train_decresed.csv"));
			String line;
			//Pierwszy row
			test.nextLine();
			while(test.hasNextLine()){
				line = test.nextLine();
				correct.add(Integer.valueOf(line.split(",")[1]));
			}
			
			//Pierwszy row
			line = train.nextLine();
			out.println(line);
			int res;
			while(train.hasNextLine()){
				line = train.nextLine();
				res = Integer.parseInt(line.split(",")[1]);
				if(correct.contains(res))
					out.println(line);
			}
			
			out.flush();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void separate(){
		HashMap<Integer,Integer> mapa = new HashMap<Integer,Integer>();
		try {
			Scanner in = new Scanner(new File("resources/train.csv"));
			PrintWriter train = new PrintWriter(new File("train_sep.csv"));
			PrintWriter test = new PrintWriter(new File("test_sep.csv"));
			String line;
			
			//Pierwszy row
			in.nextLine();
			int res;
			Integer val;
			while(in.hasNextLine()){
				line = in.nextLine();
				res = Integer.valueOf(line.split(",")[1]);
				val = mapa.get(res);
				if(val != null){
					val++;
					if(val%5 != 0){
						train.println(line);
					}else{
						test.println(line);
					}
					mapa.put(res,val);
				}else{
					mapa.put(res, 1);
				}
			}
			
			train.flush();
			test.flush();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
